package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.crime.*;
	import b1.scenes.detect.*;
	import b1.scenes.home.wanted.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	import flash.filters.*;
	import flash.external.*;
	import flash.media.Sound;

	public class TaskAccomplishDialog extends Sprite
	{
		private static var instance:TaskAccomplishDialog;
		private var contentContainer:Sprite;
		private var taskConfig:TaskConfig;
		
		public function TaskAccomplishDialog()
		{
			super();

			//主背景
			var TaskDialogBgr:Class = ResourceHelper.getInstance().getClassByName("TaskDialogBgr");
			addChild(new TaskDialogBgr());
		}
		
		public static function getInstance():TaskAccomplishDialog
		{
			if (!instance)
				instance = new TaskAccomplishDialog();
			return instance;
		}
		
		public function refresh(taskConfig:TaskConfig):void
		{
			if (contentContainer != null && contentContainer.parent != null)
				contentContainer.parent.removeChild(contentContainer);
				
			this.taskConfig = taskConfig;
			contentContainer = new Sprite();
			
			if (taskConfig.backgroundImage != "")
			{
				var TaskDialogBgr:Class = ResourceHelper.getInstance().getClassByName("TaskDialogBgr");
				var taskDialogBgr:Sprite = new TaskDialogBgr(); 
				contentContainer.addChild(taskDialogBgr);
				var TaskStoryBgrImage:Class = ResourceHelper.getInstance().getClassByName(taskConfig.backgroundImage);
				var taskStoryBgrImage:Sprite = new TaskStoryBgrImage();
				taskStoryBgrImage.width = taskDialogBgr.width;
				taskStoryBgrImage.height = taskDialogBgr.height;
				taskStoryBgrImage.alpha = 0.7;
				taskStoryBgrImage.mask = taskDialogBgr;
				contentContainer.addChild(taskStoryBgrImage);
				var TaskDialogBgr2:Class = ResourceHelper.getInstance().getClassByName("TaskDialogBgr2");
				var taskDialogBgr2:Sprite = new TaskDialogBgr2();
				taskDialogBgr2.x = (contentContainer.width - taskDialogBgr2.width) / 2;
				taskDialogBgr2.y = 100;
				taskDialogBgr2.alpha = 0.7; 
				contentContainer.addChild(taskDialogBgr2);
			}
			
			//加载名字
			var name:TextField = new TextField();
			name.selectable = false;
			name.autoSize = TextFieldAutoSize.LEFT;
			var textFormat1:TextFormat = new TextFormat("微软雅黑", 30, 0x930000, true);
			textFormat1.letterSpacing = 3; 
			name.defaultTextFormat = textFormat1;
			name.text = "完成任务";
			name.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			name.x = (this.width - name.width) / 2;
			name.y = 85;
			contentContainer.addChild(name);
			
			//加载任务图标
			var TaskIconImage:Class = ResourceHelper.getInstance().getClassByName(taskConfig.iconImage);
			var taskIconImage:Sprite = new TaskIconImage();
			taskIconImage.width = 100;
			taskIconImage.height = 100;
			taskIconImage.x = 90;
			taskIconImage.y = 140;
			contentContainer.addChild(taskIconImage);
			
			//加载奖励文字
			var bonus:TextField = new TextField();
			bonus.selectable = false;
			bonus.autoSize = TextFieldAutoSize.LEFT;
			var textFormat2:TextFormat = new TextFormat("微软雅黑", 20, 0x000000, true);
			textFormat2.letterSpacing = 2; 
			bonus.defaultTextFormat = textFormat2;
			bonus.text = "获得奖励：";
			bonus.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			bonus.x = 100;
			bonus.y = 280;
			contentContainer.addChild(bonus);
			
			//加载骨头图标
			var BoneImage:Class = ResourceHelper.getInstance().getClassByName("PriceBone"); 
			var boneImage:Sprite = new BoneImage();
			boneImage.width = 25;
			boneImage.height = 25;
			boneImage.x = 240;
			boneImage.y = 280;
			contentContainer.addChild(boneImage);
			
			//加载奖励骨头个数
			var boneText:TextField = new TextField();
			boneText.selectable = false;
			boneText.autoSize = TextFieldAutoSize.LEFT;
			boneText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			boneText.text = taskConfig.boneBonus.toString();
			boneText.x = boneImage.x + boneImage.width + 10;
			boneText.y = boneImage.y;
			contentContainer.addChild(boneText);
						
			//加载经验图标
			var ExpImage:Class = ResourceHelper.getInstance().getClassByName("ExpIcon"); 
			var expImage:Sprite = new ExpImage();
			expImage.width = 25;
			expImage.height = 25;
			expImage.x = 340;
			expImage.y = 280;
			contentContainer.addChild(expImage);
			
			//加载奖励经验
			var expText:TextField = new TextField();
			expText.selectable = false;
			expText.autoSize = TextFieldAutoSize.LEFT;
			expText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			expText.text = taskConfig.expBonus.toString();
			expText.x = expImage.x + expImage.width + 10;
			expText.y = expImage.y;
			contentContainer.addChild(expText);
						
			//加载具体描述
			var description:TextField = new TextField();
			description.selectable = false;
			description.autoSize = TextFieldAutoSize.CENTER;
			description.width = 250;
			description.wordWrap = true;
			var format1:TextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			format1.letterSpacing = 2;
			description.defaultTextFormat = format1;
			description.text = taskConfig.accomplishedDescription;
			description.x = 220;
			description.y = 180;
			contentContainer.addChild(description);
			
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - closeButton.width - 50;
			closeButton.y = 15;
			contentContainer.addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			
			//分享按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			var shareButton:BasicButton = new BasicButton(ButtonBgr, "分享");
			shareButton.x = (this.width  - shareButton.width) / 2;
			shareButton.y = this.height - shareButton.height - 110;
			shareButton.addEventListener(MouseEvent.CLICK, onShareButtonClick);
			contentContainer.addChild(shareButton);
		
			addChild(contentContainer);
		} 
		
		public function open(parentScene:Sprite, taskConfig:TaskConfig):void
		{
			if (!parentScene.contains(this))
			{
				refresh(taskConfig);
				this.x = (BFont.stageWidth - this.width) / 2;
				this.y = 60;
				parentScene.addChild(this);
				//播放音效
				var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
				var sound:Sound = new SoundButton1();
				sound.play(); 
			}
			CrimeDialog.getInstance().close();
			DetectDiaglog.getInstance().close();
			WantedTab.getInstance().close();
			BottomNavigator.getInstance().hide();
			//添加新手导引
			if (SceneContainer.getInstance().step == 32)
				SceneContainer.getInstance().addStep();
		}
		
		private function onShareButtonClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			ExternalInterface.call("sendFeed", taskConfig.feedTitle, taskConfig.feedDescription);
			onCloseButtonClick(event);
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			if (!(SceneContainer.getInstance().currentScene is Home))
				return;
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			//主线任务
			if (taskConfig.type == 0)
			{
				if (me.accomplishedTaskId + 1 < Config.taskConfigs.length)
				{
					var nextTaskConfig:TaskConfig = Config.taskConfigs[me.accomplishedTaskId + 1];
					if (nextTaskConfig.shineTaskIconFlag)
					{
						(home.icons[0] as BasicIcon).startShine();
					}
				}
			}
			//节日任务
			else
			{
				if (me.accomplishedFestivalTaskId + 1 < Config.festivalSpecialCaseConfigs.length)
				{
					nextTaskConfig = Config.festivalTaskConfigs[me.accomplishedFestivalTaskId + 1];
					if (nextTaskConfig.shineTaskIconFlag)
					{
						(home.icons[6] as BasicIcon).startShine();
					}
				}
			}
			close();
		}
		
		public function close():void
		{
			//添加新手导引
			if (SceneContainer.getInstance().step == 33)
				SceneContainer.getInstance().addStep();
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}